<?php
namespace  Admin\Controller;
class HomePageController extends AdminController{
    public function index(){
        $M_home_data=M('home_data');
        
        $list=$this->lists('home_data');
        int_to_string($list,array('position'=>array(1=>"1F",2=>"2F",3=>"3F",4=>"4F",5=>"5F")));
        $this->assign('_list',$list);
        $this->display();
        
    }
    public function edit(){
        if(IS_POST){
            $M_home_data=M('home_data');
            $data = params_request(array('title', 'position', 'sort','cover_id', 'product_id'));
            $data['update_time'] = NOW_TIME;
            !preg_match('/^.{0,50}$/', $data['title']) && $this->error('标题长度不能超过50个字符');
            !preg_match('/^[1-4]$/', $data['position']) && $this->error('选择位置');
            
            $id=I('post.id');            
            if($id){
                
                $M_home_data->where(array('id'=>$id))->save($data) ? $this->success('保存成功',U('index')) : $this->error('保存失败');
            }
            else{
                $data['create_time'] = NOW_TIME;
                
                $M_home_data->add($data) ? $this->success('添加成功',U('index')) : $this->error('添加失败');
            }
           
        }else {
            $id=I('id');
            $M_home_data=M('home_data');
            $this->assign('info',$M_home_data->find($id));
            $this->display();
        }
    }
    public function del($id){
       M('home_data')->where(array('id'=>$id))->delete() ? $this->success('删除成功') : $this->error('删除失败');
    }
    
    
}

?>